package com.dog.basics.common.util;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.ObjectUtil;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @description: 图片处理
 * @author: He
 * @date: Created in 2020/2/27 12:30
 * @version: 1.0
 * @modified By:
 */
public class ImageUtil {

    /**
     * 网络图片转为byte数组
     *
     * @param path
     * @return
     */
    public static byte[] image2byte(String path) throws IOException {
        byte[] data = null;
        URL url = null;
        InputStream input = null;
        try {
            url = new URL(path);
            HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
            httpUrl.connect();
            httpUrl.getInputStream();
            input = httpUrl.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int numBytesRead = 0;
        while ((numBytesRead = input.read(buf)) != -1) {
            output.write(buf, 0, numBytesRead);
        }
        data = output.toByteArray();
        output.close();
        input.close();
        return data;
    }

    /**
     * 获取图片 - 需要追加图片文字的宽度有多少
     *
     * @param image
     * @param pressText
     * @param color
     * @param font
     * @param noWidth
     * @return guonh 2020年2月29日
     */
    public static int imgContentWidthOrHeight(Image image, String pressText, Color color, Font font, Boolean noWidth) {
        final BufferedImage targetImage = ImgUtil.toBufferedImage(image);
        final Graphics2D g = targetImage.createGraphics();
        if (null == font) {
            // 默认字体
            font = new Font("Courier", Font.PLAIN, (int) (targetImage.getHeight() * 0.75));
        }
        // 抗锯齿
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(color);
        g.setFont(font);
        // 在指定坐标绘制水印文字
        final FontMetrics metrics = g.getFontMetrics(font);
        final int textLength = metrics.stringWidth(pressText);
        final int textHeight = metrics.getAscent() - metrics.getLeading() - metrics.getDescent();
        if (ObjectUtil.isNotNull(noWidth) && noWidth) {
            return textLength;
        } else {
            return textHeight;
        }
    }


    /**
     * 计算 - 向左靠多少
     *
     * @param textLength
     * @return guonh 2020年2月29日
     */
    public static int calculationLeanToTheLeft(int textLength) {
        return BigDecimal.valueOf(-800).add(BigDecimal.valueOf(textLength).divide(BigDecimal.valueOf(2))).intValue();
    }

    /**
     * 自动计算获取字宽度 - 与向左去多少
     *
     * @param image
     * @param pressText
     * @param color
     * @param font
     * @param noWidth
     * @return guonh 2020年2月29日
     */
    public static int calculationLeanToTheLeftWidth(Image image, String pressText, Color color, Font font, Boolean noWidth) {
        return calculationLeanToTheLeft(imgContentWidthOrHeight(image, pressText, color, font, noWidth));
    }

    /**
     * InputStream 转换 ByteArrayInputStream
     *
     * @param image
     * @return
     * @throws IOException
     */
    public static byte[] toInputStreamByBufferedImage(BufferedImage image, String prefix) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, prefix, os);
        return os.toByteArray();

    }

}